'use strict';

import { Device, DeviceEvent, Host, PackageEvent } from "miot";
import React from 'react';
import { ActionSheetIOS, Image, ListView, PixelRatio, StyleSheet, Text, TouchableHighlight, View } from 'react-native';
import Logger from '../../Logger';

let BUTTONS = [
  '测试对话框',
  '确定'
];

export default class UIDemo extends React.Component {

  constructor(props) {
    super(props);
    let ds = new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2 });
    this._createMenuData();
    this.state = { dataSource: ds.cloneWithRows(this._menuData.map((o) => ({ 'name': o.name, 'subtitle': o.subtitle }))) };
    Logger.trace(this);
  }

  componentDidMount() {
    this._multiSwitchNameChangedListener = DeviceEvent.multiSwitchNameChanged.addListener((value, did) => {
      console.log(`switch name changed: ${ JSON.stringify(value) }`, 'did: ', did);
      alert(JSON.stringify(value));
    });
  }

  componentWillUnmount() {
    this._multiSwitchNameChangedListener && this._multiSwitchNameChangedListener.remove();
  }

  _createMenuData() {
    this._menuData = [
      {
        'name': '多键开关设置',
        'subtitle': 'openPowerMultikeyPage',
        'func': () => {
          // Host.ui.openWebPage('http://s.miwifi.com/dist/userhosts/index.html');
          
          Host.ui.openPowerMultikeyPage(Device.deviceID, Device.mac, { useNewSetting: true });
        }
      },
      {
        'name': '用户协议与隐私政策',
        'subtitle': 'navigate 到 PrivacyDemo',
        'func': () => {
          this.props.navigation.navigate('PrivacyDemo', { 'title': '用户协议与隐私政策' });
        }
      },
      {
        'name': '是否支持商城',
        'subtitle': 'canOpenStorePage',
        'func': () => {
          Host.ui.canOpenStorePage().then((isSupport) => {
            alert(`是否支持商城: ${ isSupport }`);
          }).catch((error) => {
            console.log(error);
          });
        }
      },
      {
        'name': '弹出删除设备的对话框',
        'subtitle': 'openDeleteDevice',
        'func': () => {
          Host.ui.openDeleteDevice('title test');
        }
      },
      {
        'name': '弹出删除设备的对话框(带回调)',
        'subtitle': 'openDeleteDeviceWithCallback',
        'func': () => {
          Host.ui.openDeleteDeviceWithCallback()
            .then((res) => {
              console.log(res);
            })
            .catch((err) => {
              console.log(err);
            });
        }
      },
      {
        'name': '打开分享设备的页面',
        'subtitle': 'openShareDevicePage',
        'func': () => {
          Host.ui.openShareDevicePage();
        }
      },
      {
        'name': '保持屏幕常亮',
        'subtitle': 'keepScreenNotLock(true)',
        'func': () => {
          Host.ui.keepScreenNotLock(true);
        }
      },
      {
        'name': '关闭保持屏幕常亮',
        'subtitle': 'keepScreenNotLock(false)',
        'func': () => {
          Host.ui.keepScreenNotLock(false);
        }
      },
      {
        'name': '打开房间设备管理的页面',
        'subtitle': 'openRoomManagementPage',
        'func': () => {
          Host.ui.openRoomManagementPage();
        }
      },
      {
        'name': '打开语音设备管理的页面',
        'subtitle': 'openVoiceCtrlDeviceAuthPage',
        'func': () => {
          Host.ui.openVoiceCtrlDeviceAuthPage();
        }
      },
      {
        'name': '打开反馈页',
        'subtitle': 'openFeedbackInput',
        'func': () => {
          Host.ui.openFeedbackInput();
        }
      },
      {
        'name': '打开安全管理页',
        'subtitle': 'openSecuritySetting',
        'func': () => {
          Host.ui.openSecuritySetting();
        }
      },
      {
        'name': '打开常见问题页，别名「使用帮助」',
        'subtitle': 'openHelpPage',
        'func': () => {
          Host.ui.openHelpPage();
        }
      },
      {
        'name': '打开分享列表页面',
        'subtitle': 'openShareListBar',
        'func': () => {
          Host.ui.openShareListBar(
            '小米智能家庭',
            '小米智能家庭',
            { uri: 'https://avatars3.githubusercontent.com/u/13726966?s=40&v=4' },
            'https://iot.mi.com/new/index.html'
          );
        }
      },
      {
        'name': '打开系统分享文件页面',
        'subtitle': 'openSystemShareWindow',
        'func': () => {
          // const path = Host.file.storageBasePath+'/test.wav';
          Host.ui.openSystemShareWindow('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586433650723&di=44f673bd1ccbde2af32e1f7565de4484&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2Fw640h759%2F20180111%2F9ee0-fyqnick6733536.jpg');
        }
      },
      {
        'name': '打开系统文件打开页面',
        'subtitle': 'openSystemFileWindow',
        'func': () => {
          Host.ui.openSystemFileWindow('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1586433650723&di=44f673bd1ccbde2af32e1f7565de4484&imgtype=0&src=http%3A%2F%2Fn.sinaimg.cn%2Fsinacn%2Fw640h759%2F20180111%2F9ee0-fyqnick6733536.jpg');
        }
      },
      {
        'name': '获取设备列表中指定model的设备信息',
        'subtitle': 'getDevicesWithModel',
        'func': () => {
          Host.ui.getDevicesWithModel(Device.model)
            .then((res) => {
              alert(`success${ JSON.stringify(res) }`);
            })
            .catch((err) => {
              alert(`err ${ err }`);
            });
        }
      },
      {
        'name': '打开蓝牙网关页',
        'subtitle': 'openBtGatewayPage',
        'func': () => {
          Host.ui.openBtGatewayPage();
        }
      },
      {
        'name': '弹窗请求隐私政策和用户协议授权， 支持显示用户体验计划',
        'subtitle': 'alertLegalInformationAuthorization',
        'func': () => {
          const licenseURL = require('../../../Resources/raw/license_zh.html');
          const privacyURL = require('../../../Resources/raw/privacy_zh.html');
          let options = {};
          options.agreementURL = licenseURL;
          options.privacyURL = privacyURL;
          options.experiencePlanURL = licenseURL;
          options.hideAgreement = false;
          options.hideUserExperiencePlan = false;
          // 这是为了演示需要，使用的是强制弹出的模式 具体使用方法可以参考iot文档 或 com.xiaomi.demo/MainPage.js部分样例
          Host.ui.alertLegalInformationAuthorization(options).then(() => {
            alert('成功');
          }).catch(() => {
            alert('失败，可能是设备已离线');
          });
        }
      },
      {
        'name': '查看隐私政策和用户协议信息， 支持显示用户体验计划',
        'subtitle': 'previewLegalInformationAuthorization',
        'func': () => {
          const licenseURL = require('../../../Resources/raw/license_zh.html');
          const privacyURL = require('../../../Resources/raw/privacy_zh.html');
          let options = {};
          options.agreementURL = licenseURL;
          options.privacyURL = privacyURL;
          options.experiencePlanURL = licenseURL;
          options.hideAgreement = false;
          options.hideUserExperiencePlan = false;
          Host.ui.previewLegalInformationAuthorization(options).then(() => {
            alert('成功');
          }).catch(() => {
            alert('失败，可能是设备已离线');
          });
        }
      },
      {
        'name': '打开重命名对话框',
        'subtitle': 'openChangeDeviceName',
        'func': () => {
          Host.ui.openChangeDeviceName();
        }
      },
      {
        'name': '添加桌面快捷方式',
        'subtitle': 'openAddToDesktopPage',
        'func': () => {
          Host.ui.openAddToDesktopPage();
        }
      },
      {
        'name': '打开设备检查固件升级页(新版页面)',
        'subtitle': 'openDeviceUpgradePage(0)',
        'func': () => {
          Host.ui.openDeviceUpgradePage(0);
        }
      },
      {
        'name': '打开设备检查固件升级页(旧版页面)',
        'subtitle': 'openDeviceUpgradePage(1)',
        'func': () => {
          Host.ui.openDeviceUpgradePage(1);
        }
      },
      {
        'name': '打开Mesh设备固件升级页。分享的设备点击此接口无反应',
        'subtitle': 'openBleMeshDeviceUpgradePage 只有mesh设备才能打开',
        'func': () => {
          Host.ui.openBleMeshDeviceUpgradePage();
        }
      },
      {
        'name': '打开通用协议的蓝牙固件OTA页面。分享的设备点击此接口无反应',
        'subtitle': 'openBleCommonDeviceUpgradePage',
        'func': () => {
          alert(' 蓝牙部分api请参考 com.xiaomi.bledemo 中的示例使用');
        }
      },
      {
        'name': '打开灯组2.0固件升级页。分享的设备点击此接口无反应',
        'subtitle': 'openLightGroupUpgradePage',
        'func': () => {
          Host.ui.openLightGroupUpgradePage();
        }
      },
      {
        'name': '打开设备时区设置页',
        'subtitle': 'openDeviceTimeZoneSettingPage  {"sync_device": false} ',
        'func': () => {
          Host.ui.openDeviceTimeZoneSettingPage({ "sync_device": false });
        }
      },
      {
        'name': '打开网关设备产品百科页面',
        'subtitle': 'openProductBaikeWebPage 网关设备',
        'func': () => {
          let url = 'https://home.mi.com/baike/index.html#/label/gateway';
          Host.ui.openProductBaikeWebPage(url);
        }
      },
      {
        'name': '打开商城某商品详情页面',
        'subtitle': 'openShopPage 小米台灯',
        'func': () => {
          Host.ui.openShopPage(102763);
        }
      },
      {
        'name': '打开有品商城搜索结果页面',
        'subtitle': 'openShopSearchPage(\'小米台灯\')',
        'func': () => {
          Host.ui.openShopSearchPage('小米台灯');
        }
      },
      {
        'name': '打开一次性密码设置页',
        'subtitle': 'openOneTimePassword',
        'func': () => {
          Host.ui.openOneTimePassword(Device.deviceID, 30, 6);
        }
      },
      {
        'name': '打开默认倒计时页面',
        'subtitle': 'openCountDownPage',
        'func': () => {
          Host.ui.openCountDownPage(true,
            {
              onMethod: "power_on",
              offMethod: 'power_off',
              onParam: 'on',
              offParam: 'off'
            });
        }
      },
      {
        'name': '打开自定义设置定时页面-Demo1',
        'subtitle': 'openTimerSettingPageWithOptions',
        'func': () => {
          Host.ui.openTimerSettingPageWithOptions(
            {
              onMethod: "power_on",
              onParam: "on",
              offMethod: "power_off",
              offParam: "off",
              timerTitle: "这是一个自定义标题",
              displayName: "自定义场景名称"
            });
        }
      },
      {
        'name': '打开自定义设置定时页面-Demo2',
        'subtitle': 'openTimerSettingPageWithOptions',
        'func': () => {
          let params = {
            onMethod: "power_on",
            onParam: "on",
            offMethod: "power_off",
            offParam: "off",
            timerTitle: "这是一个自定义标题",
            displayName: "自定义场景名称",
            identify: "identify_1",
            onTimerTips: '',
            offTimerTips: '定时列表页面、设置时间页面 关闭时间副标题（默认：关闭时间）',
            listTimerTips: '定时列表页面 定时时间段副标题（默认：开启时段）',
            bothTimerMustBeSet: false,
            showOnTimerType: true,
            showOffTimerType: false,
            showPeriodTimerType: false
          };
          Host.ui.openTimerSettingPageWithOptions(params);
        }
      },
      {
        'name': '打开一个原生类 className（只支持iOS）',
        'subtitle': 'openPageWithClassName',
        'func': () => {
          Host.ui.openPageWithClassName('MHDeviceTimerSettingNewViewController');
        }
      },
      {
        'name': '打开手机蓝牙设置页面（只支持android）',
        'subtitle': 'openPhoneBluSettingPage',
        'func': () => {
          Host.ui.openPhoneBluSettingPage();
        }
      },
      {
        'name': '显示打开蓝牙引导(仅ios)',
        'subtitle': 'showBLESwitchGuide',
        'func': () => {
          Host.ui.showBLESwitchGuide();
        }
      },
      {
        'name': '隐藏提示用户打开蓝牙的动画示意图(仅ios)',
        'subtitle': 'dismissBLESwitchGuide',
        'func': () => {
          Host.ui.dismissBLESwitchGuide();
        }
      },
      {
        'name': '打开快连成功页面',
        'subtitle': 'openConnectSucceedPage',
        'func': () => {
          Host.ui.openConnectSucceedPage(Device.model, Device.deviceID);
        }
      },
      {
        'name': '打开添加Zigbee网关插件子设备页面',
        'subtitle': 'openZigbeeConnectDeviceList',
        'func': () => {
          Host.ui.openZigbeeConnectDeviceList(Device.deviceID);
        }
      },
      {
        'name': '打开设备网络信息页面 此方法只针对wifi设备，combo设备，蓝牙设备请不要调用此方法。',
        'subtitle': 'openDeviceNetworkInfoPage',
        'func': () => {
          Host.ui.openDeviceNetworkInfoPage();
        }
      },
      {
        'name': '跳转到小米钱包（仅Android）',
        'subtitle': 'openMiPayPageForAndroid',
        'func': () => {
          let params = {
            action: 'issue_mifare',
            type: '1',
            product_id: '66666-00211',
            source_channel: 'mijia'
          };
          Host.ui.openMiPayPageForAndroid(params).then((res) => {
            console.log(res);
          }).catch((error) => {
            console.log(error);
          });
        }
      },
      {
        'name': '跳转到设备定向推荐界面',
        'subtitle': 'getRecommendScenes & openPluginRecommendScene',
        'func': () => {
          // TODO ios返回了带code的res，待anroid确认是不是统一
          // recommendId 通过 Device.getRecommendScenes(Device.model, Device.deviceID).then 来获取
          Device.getDeviceWifi().getRecommendScenes(Device.model, Device.deviceID).then((res) => {
            if (res.scene_recom && res.scene_recom.length > 0 && res.scene_recom[0] && res.scene_recom[0].info) {
              console.log("res", res.scene_recom[0].info.sr_id);
              Host.ui.openPluginRecommendScene(Device.deviceID, parseInt(res.scene_recom[0].info.sr_id));
            } else {
              alert(`res: ${ JSON.stringify(res) }`);
            }
          }).catch((error) => {
            alert(`error: ${ error }`);
          });
        }
      },
      {
        'name': '刷新设备列表',
        'subtitle': 'refreshDeviceList',
        'func': () => {
          Host.ui.refreshDeviceList().then((res) => {
            alert(res);
          }).catch((error) => {
            alert(JSON.stringify(error));
          });
        }
      },
      {
        'name': 'openTerminalDeviceSettingPage',
        'subtitle': '打开手机设置页中米家app配置页面',
        'func': () => {
          Host.ui.openTerminalDeviceSettingPage(1);
        }
      },
      {
        'name': 'openTerminalDeviceSettingPage',
        'subtitle': '打开手机WiFi设置页面',
        'func': () => {
          Host.ui.openTerminalDeviceSettingPage(2);
        }
      },
      {
        'name': 'checkAndroidLocationServerIsOpen',
        'subtitle': '检测手机位置服务是否打开(only Android)',
        'func': () => {
          Host.checkAndroidLocationServerIsOpen().then((result) => {
            console.log(result.data.locationServerIsOpen);
            alert(JSON.stringify(result));
          }).catch((error) => {
            alert(JSON.stringify(error));
          });
        }
      },
      {
        'name': 'openAndroidLocationServerSettingPage',
        'subtitle': '打开手机系统位置信息设置页(only Android)',
        'func': () => {
          Host.ui.openAndroidLocationServerSettingPage();
        }
      },
      {
        'name': 'getIOSLocationAuthorizationStatus',
        'subtitle': '获取定位授权状态(only iOS)',
        'func': () => {
          Host.getIOSLocationAuthorizationStatus().then((result) => {
            alert(`成功:${ JSON.stringify(result) }`);
          }).catch((error) => {
            alert(`失败:${ JSON.stringify(error) }`);
          });
        }
      },
      {
        'name': '监听跳转到Native页面',
        'subtitle': 'iOS Only',
        'func': () => {
          this.disappearListener = PackageEvent.packageViewWillDisappearIOS.addListener(() => {
            alert("packageViewWillDisappearIOS");
            this.disappearListener && this.disappearListener.remove();
            this.disappearListener = null;
          });
          Host.ui.openNewMorePage();
        }
      },
      {
        'name': 'checkAbilityOfJumpToThirdpartyApplication',
        'subtitle': '判断是否可以跳到其他App',
        'func': () => {
          Host.checkAbilityOfJumpToThirdpartyApplication("mihome://plugin")
            .then((res) => {
              alert(JSON.stringify(res));
            })
            .catch((error) => {
              alert(JSON.stringify(error));
            });
        }
      },
      {
        'name': 'jumpToThirdpartyApplication',
        'subtitle': '跳转到其他App',
        'func': () => {
          let params = {
            action: 'issue',
            type: 'MIFARE_ENTRANCE',
            source_channel: 'MiHome',
            product_id: '66666-00211',
            model: 'baiji'
          };
          // mihome://plugin?action=issue&type=MIFARE_ENTRANCE&source_channel=MiHome&product_id=66666-00211&model=xxxx&pass_through=B64String
          let passThrough = params;
          Host.jumpToThirdpartyApplication("mihome://plugin", params, passThrough)
            .then((res) => {
              alert(JSON.stringify(res));
            })
            .catch((error) => {
              alert(JSON.stringify(error));
            });
        }
      },
      {
        'name': 'openIOSDocumentFileChoosePage',
        'subtitle': '打开文件选择页面 only for iOS',
        'func': () => {
          if (Host.isIOS) {
            Host.ui.openIOSDocumentFileChoosePage().then((res) => {
              alert(JSON.stringify(res));
            }).catch((err) => {
              alert(JSON.stringify(err));
            });
          }
        }
      },
      {
        'name': 'openConnectSucceedPage',
        'subtitle': '跳转到房间设置的页面',
        'func': () => {
          Host.ui.openConnectSucceedPage(Device.model, Device.deviceID);
        }
      },
      {
        'name': 'openTerminalDeviceSettingPage',
        'subtitle': '跳转到WiFi选择页面 ',
        'func': () => {
          Host.ui.openTerminalDeviceSettingPage(3);
        }
      },
      {
        'name': 'openBleGroupUpgradePage',
        'subtitle': '跳转到蓝牙组设备批量升级页面-type 为0-普通小米蓝牙协议设备',
        'func': () => {
          Host.ui.openBleGroupUpgradePage(0);
        }
      },
      {
        'name': 'openBleGroupUpgradePage',
        'subtitle': '跳转到蓝牙组设备批量升级页面-type 为1-安全芯片小米蓝牙设备',
        'func': () => {
          Host.ui.openBleGroupUpgradePage(1);
        }
      },
      {
        'name': 'openBleGroupUpgradePage',
        'subtitle': '跳转到蓝牙组设备批量升级页面-type 为2-分享的安全芯片小米蓝牙设备',
        'func': () => {
          Host.ui.openBleGroupUpgradePage(2);
        }
      },
      {
        'name': 'openBleGroupUpgradePage',
        'subtitle': '跳转到蓝牙组设备批量升级页面-type 为3-普通的BLE蓝牙设备',
        'func': () => {
          Host.ui.openBleGroupUpgradePage(3);
        }
      },
      {
        'name': 'openBleGroupUpgradePage',
        'subtitle': '跳转到蓝牙组设备批量升级页面-type 为4-标准蓝牙认证协议',
        'func': () => {
          Host.ui.openBleGroupUpgradePage(4);
        }
      },
      {
        'name': 'openBleGroupUpgradePage',
        'subtitle': '跳转到蓝牙组设备批量升级页面-type 为5-mesh',
        'func': () => {
          Host.ui.openBleGroupUpgradePage(5);
        }
      },
      {
        'name': 'openNFCWriteDeviceInfoPage',
        'subtitle': '打开NFC写设备信息页面',
        'func': () => {
          Host.ui.openNFCWriteDeviceInfoPage();
        }
      },
      {
        'name': 'openNFCWriteDeviceInfoDebugPage',
        'subtitle': '打开NFC写设备信息Debug页面',
        'func': () => {
          let params = {
            did: Device.deviceID,
            model: Device.model,
            extra: JSON.stringify({ key: 'test123' })
          };
          Host.ui.openNFCWriteDeviceInfoDebugPage(params);
        }
      },
      {
        'name': 'openCommonDeviceSettingPage',
        'subtitle': '打开常用设备/常用摄像机设置页面',
        'func': () => {
          Host.ui.openCommonDeviceSettingPage(1);
        }
      },
      {
        'name': 'openGenerateCrontabStringPage',
        'subtitle': '打开场景设置中的定时界面',
        'func': () => {
          Host.ui.openGenerateCrontabStringPage({ title: 'demo标题', hideLegalTime: false })
            .then((crontab) => {
              alert(crontab.data.crontab);
            });
        }
      },
      {
        'name': 'openFirmWareAutoOTAPage',
        'subtitle': '打开设置-检查更新中的固件自动更新',
        'func': () => {
          Host.ui.openFirmWareAutoOTAPage();
        }
      }
    ];
  }

  render() {
    return (
      <View style={styles.container}>
        <ListView style={styles.list} dataSource={this.state.dataSource} renderRow={this._renderRow.bind(this)} />
      </View>
    );
  }

  _renderRow(rowData, sectionID, rowID) {
    return (
      <TouchableHighlight underlayColor="#838383" onPress={() => this._pressRow(rowID)}>
        <View>
          <View style={styles.rowContainer}>
            <View style={{ flex: 1 }}>
              <Text style={styles.title}>{rowData.name}</Text>
              <Text style={styles.subtitle}>{rowData.subtitle}</Text>
            </View>
            <Image style={styles.subArrow} source={require("../../../Resources/sub_arrow.png")} />
          </View>
          <View style={styles.separator}></View>
        </View>
      </TouchableHighlight >
    );
  }

  _pressRow(rowID) {
    console.log(`row${ rowID }clicked!`);
    this._menuData[rowID].func();
    Logger.trace(this, this._pressRow, this._menuData[rowID]);
  }

  showActionSheet() {
    if (Host.isIOS)
      ActionSheetIOS.showActionSheetWithOptions({
        options: BUTTONS,
        destructiveButtonIndex: 1
      },
      () => { });
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    borderTopColor: '#f1f1f1',
    borderTopWidth: 1,
    flexDirection: 'row',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#ffffff',
    marginBottom: 0,
    marginTop: 0
  },

  rowContainer: {
    height: 62,
    alignSelf: 'stretch',
    flexDirection: 'row',
    paddingLeft: 23,
    paddingRight: 23,
    alignItems: 'center',
    flex: 1
  },
  list: {
    alignSelf: 'stretch',
    marginBottom: 20
  },
  title: {
    fontSize: 15,
    color: '#333333',
    alignItems: 'center'
  },
  subtitle: {
    fontSize: 12,
    color: '#666666',
    alignItems: 'center'
  },
  subArrow: {
    width: 7,
    height: 14
  },
  separator: {
    height: 1 / PixelRatio.get(),
    backgroundColor: '#e5e5e5',
    marginLeft: 20
  }
});
